package com.wmtiger.utils.math
{
	import flash.geom.Point;

	public class DirectionUtil
	{
		public function DirectionUtil()
		{
		}
		
		public static function get4Dir(s_p : Point, e_p : Point):int
		{
			var angle : Number;
			var dir : int;
			var diff : int = 45;
			angle = Math.atan2(s_p.y - e_p.y, s_p.x - e_p.x) / Math.PI * 180 + 180;
			if (angle > 360 - diff || angle <= diff)
			{
				dir = 6;
			}
			else if (angle > diff && angle <= 90 + diff)
			{
				dir = 2;
			}
			else if (angle > 90 + diff && angle <= 180 + diff)
			{
				dir = 4;
			}
			else if (angle > 180 + diff && angle <= 360 - diff)
			{
				dir = 8;
			}
			return dir;
		}
		
		public static function get8Dir(s_p : Point, e_p : Point):int
		{
			var angle : Number;
			var diff : int;
			var dir : int;
			angle = Math.atan2(s_p.y - e_p.y, s_p.x - e_p.x) / Math.PI * 180 + 180;
			diff = -22.5;
			if (angle > 360 + diff || angle <= 45 + diff)
			{
				dir = 6;
			}
			else if (angle > 45 + diff && angle <= 90 + diff)
			{
				dir = 3;
			}
			else if (angle > 90 + diff && angle <= 135 + diff)
			{
				dir = 2;
			}
			else if (angle > 135 + diff && angle <= 180 + diff)
			{
				dir = 1;
			}
			else if (angle > 180 + diff && angle <= 225 + diff)
			{
				dir = 4;
			}
			else if (angle > 225 + diff && angle <= 270 + diff)
			{
				dir = 7;
			}
			else if (angle > 270 + diff && angle <= 315 + diff)
			{
				dir = 8;
			}
			else if (angle > 315 + diff || angle <= 360 + diff)
			{
				dir = 9;
			}
			return dir;
		}
		
	}
}